#include <bits/stdc++.h>

#define int long long

using namespace std;

int jc(int x)
{
    return (x * (x + 1)) / 2;
}

void solve()
{
    int n, k;
	cin >> n >> k;
	vector<int> a(n + 1), v(n + 1);
	for(int i = 1; i <= n; i++) 
	{
		cin >> a[i];
		v[a[i]]++;
	}
	for(int i = 1; i <= n; i++)
	{
		if(v[i] == 0) continue;
		if(v[i] < k || v[i] % k)
		{
			cout << 0 << '\n';
			return;
		}
	}
    int ans = 0;
    vector<int> mp(n + 1);
    int l = 1,r = 1;
    while(r <= n)
	{
		mp[a[r]]++;
		while(mp[a[r]] > v[a[r]] / k) 
		{
			mp[a[l]]--;
			l++;
		}
		ans += (r - l + 1);
		r++;
	}
    cout << ans << endl;
}

signed main()
{
    int t;
    cin >> t;
    while(t --)
    {
        solve();
    }
}